//
//  HomeTableViewController.swift
//  Sina
//
//  Created by 王杰 on 15/11/11.
//  Copyright © 2015年 王杰. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
private let HomeCellId = "HomeCellId"
class HomeTableViewController: BaseTableViewController {
    //懒加载数据源
    private lazy var statuses = [Status]()
    
    override func viewDidLoad() {
        
        if !userLogin {
            visitorLoginView?.setUIInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }

        //准备加载一个tableView
        prepareTableView()
        loadData()

    }
    
    private func prepareTableView() {
        //设置下拉刷新
        refreshControl = UIRefreshControl()
        //添加监听方法
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
    tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellId)
        
//        tableView.rowHeight = 200
        tableView.separatorStyle = .None
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.tableFooterView = indicatorView
    }
    @objc private func loadData() {
        //确定since_id ,max_id
//        let since_id = statuses.first?.id ?? 0
//        let max_id = statuses.last?.id ?? 0

        var since_id = 0
        var max_id = 0
        if indicatorView.isAnimating() {
        
            since_id = 0
            max_id = statuses.last?.id ?? 0
            
        }else {
        
            max_id = 0
            since_id = statuses.first?.id ?? 0
        }
        
        //加载数据
        StatusListViewModel.loadData(since_id: since_id, max_id: max_id) { (array) -> () in
            //结束菊花的转动
            self.refreshControl?.endRefreshing()
            
            if array == nil {
                //请求失败
            SVProgressHUD.showErrorWithStatus(AppErrorTip)
           //请求数据失败 停止动画
                self.indicatorView.stopAnimating()
                
                return
            }
            
            if since_id > 0 {
            
            self.statuses = array! + self.statuses
                
            }else if max_id > 0 {
            //加载数据完成后停止转动
            self.statuses += array!
                
                self.indicatorView.stopAnimating()
            
            } else {
            
                self.statuses = array!
            }
            //刷新表格
            self.tableView.reloadData()
        }
        
    }
    //懒加载菊花控件
    private lazy var indicatorView: UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        
        return indicator
    }()

    
}

extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! StatusCell
        
//        cell.status = statuses[indexPath.row]
        let status = statuses[indexPath.row]
        cell.status = status
        if indexPath.row == statuses.count - 1 && !indicatorView.isAnimating() {
        
        indicatorView.startAnimating()
            loadData()
        }
        return cell
    }
}
